import Vue from 'vue'
import Vuex from 'vuex'
import cart from './modules/cart.js'
import address from './modules/address.js'
import order from './modules/order.js'
import details from './modules/details.js'

Vue.use(Vuex)

// 将购物车的信息存到浏览器
const myPlugin = store => {
	store.subscribe((mutations, state) => {
		// console.log(mutations, state)
		if(mutations.type.startsWith('cart/')) {
			localStorage.setItem('cart', JSON.stringify(state.cart.cartProducts))
		} else if(mutations.type.startsWith('address/')) {
			localStorage.setItem('address', JSON.stringify(state.address.cityAddressArr))
		} else if(mutations.type.startsWith('order/')) {
			localStorage.setItem('orderlist', JSON.stringify(state.order.orderList))
		}
	})
}


export default new Vuex.Store({
  	plugins:[myPlugin],
  	state: {
  	},
  	mutations: {
  	},
  	actions: {
  	},
  	modules: {
		cart,
		address,
		order,
		details
  	}
})
